from rest_framework.generics import ListCreateAPIView
from rest_framework.permissions import IsAdminUser
from apps.users.models import User
from apps.meiduo_admin.serialziers.users import UserSerializer
from apps.meiduo_admin.utils import NumPage


class UsersViews(ListCreateAPIView):
    # 指定查询集
    queryset = User.objects.filter(is_staff=False)
    # 指定序列化器
    serializer_class = UserSerializer
    # 指定权限
    permission_classes = [IsAdminUser]
    # 指定分页
    pagination_class = NumPage

    # 重写获取数据方法，根据不同数据获取不同的内容
    def get_queryset(self):
        # 获取keyword关键字
        key_world = self.request.query_params.get('keyword')
        # 根据关键字返回不同数据内容
        if key_world == '':
            return self.queryset
        else:
            return User.objects.filter(username__contains=key_world)

